package com.example.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.example.base.Action.BaseAction;
import com.example.base.ResultVO;
import com.example.entity.Machine;
import com.example.entity.ProductType;
import com.example.model.StatisticsModel;
import com.example.repository.ProductTypeRepository;
import com.example.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.entity.DailySalesStatistics;
import com.example.repository.DailySalesStatisticsRepository;

/**
 * Created by Administrator on 2019/5/29.
 */
@RestController
@RequestMapping("/dailySalesStatisticsAction")
public class DailySalesStatisticsAction extends BaseAction{

    @Autowired
    private DailySalesStatisticsRepository dailySalesStatisticsRepository;

    @Autowired
    private ProductTypeRepository productTypeRepository;

    @RequestMapping("/saleHome")
    public ResultVO saleHome(String time,Integer searchType){

        int sameMonthCount=0;//当月数量
        int todayCount=0;//今日数量
        int yesterdayCount=0;//昨日数量
        int stockCount=0;//库存

        List<StatisticsModel> list=new ArrayList();

        String nowDate=DateUtils.format7(new Date());

        //在售商品类型
        List<ProductType> productTypeList=productTypeRepository.findByOnSaleAndRecStatus(ProductType.ON_SALE_DISPLAY,0);

        //当月总销售数量
        int monthCount= dailySalesStatisticsRepository.selectMonthSale(time);

        if(searchType==1) {
            //出库数量
            for (ProductType pt:productTypeList){

                if(time.equals(nowDate)){
                    //当月数量
                    sameMonthCount=dailySalesStatisticsRepository.selectStockOutByDateAndProductType("%Y-%m",nowDate,pt.getId().intValue());
                    //今日数量
                    todayCount=dailySalesStatisticsRepository.selectStockOutByDateAndProductType("%Y-%m-%d",DateUtils.format10(new Date()),pt.getId().intValue());
                    //昨日数量
                    yesterdayCount=dailySalesStatisticsRepository.selectStockOutByDateAndProductType("%Y-%m-%d",DateUtils.getYesterdayByDate(),pt.getId().intValue());
                    //库存
                    stockCount=dailySalesStatisticsRepository.selectStockByProductType(Machine.STATUS_NEW,pt.getId().intValue());
                }else {
                    sameMonthCount=dailySalesStatisticsRepository.selectStockOutByDateAndProductType("%Y-%m",time,pt.getId().intValue());
                }
                StatisticsModel statisticsModel=new StatisticsModel();
                statisticsModel.setSameMonthCount(sameMonthCount);
                statisticsModel.setTodayCount(todayCount);
                statisticsModel.setYesterdayCount(yesterdayCount);
                statisticsModel.setProductTypeName(pt.getProductTypeName());
                statisticsModel.setStockCount(stockCount);
                list.add(statisticsModel);
            }

        }else if(searchType==2){
            //入库数量
            for (ProductType pt:productTypeList){
                if(time.equals(nowDate)){
                    //当月数量
                    sameMonthCount=dailySalesStatisticsRepository.selectStockInByDateAndProductType("%Y-%m",nowDate,pt.getId().intValue());
                    //今日数量
                    todayCount=dailySalesStatisticsRepository.selectStockInByDateAndProductType("%Y-%m-%d",DateUtils.format10(new Date()),pt.getId().intValue());
                    //昨日数量
                    yesterdayCount=dailySalesStatisticsRepository.selectStockInByDateAndProductType("%Y-%m-%d",DateUtils.getYesterdayByDate(),pt.getId().intValue());
                    //库存
                    stockCount=dailySalesStatisticsRepository.selectStockByProductType(Machine.STATUS_NEW,pt.getId().intValue());
                }else {
                    sameMonthCount=dailySalesStatisticsRepository.selectStockInByDateAndProductType("%Y-%m",time,pt.getId().intValue());
                }
                StatisticsModel statisticsModel=new StatisticsModel();
                statisticsModel.setSameMonthCount(sameMonthCount);
                statisticsModel.setTodayCount(todayCount);
                statisticsModel.setYesterdayCount(yesterdayCount);
                statisticsModel.setProductTypeName(pt.getProductTypeName());
                statisticsModel.setStockCount(stockCount);
                list.add(statisticsModel);
            }

        }else{
            //订单数量
            for (ProductType pt:productTypeList){

                if(time.equals(nowDate)){
                    //当月数量
                    sameMonthCount=dailySalesStatisticsRepository.selectOrderAmountByDateAndProductType("%Y-%m",nowDate,pt.getId().intValue());
                    //今日数量
                    todayCount=dailySalesStatisticsRepository.selectOrderAmountByDateAndProductType("%Y-%m-%d",DateUtils.format10(new Date()),pt.getId().intValue());
                    //昨日数量
                    yesterdayCount=dailySalesStatisticsRepository.selectOrderAmountByDateAndProductType("%Y-%m-%d",DateUtils.getYesterdayByDate(),pt.getId().intValue());
                    //库存
                    stockCount=dailySalesStatisticsRepository.selectStockByProductType(Machine.STATUS_NEW,pt.getId().intValue());
                }else {
                    sameMonthCount=dailySalesStatisticsRepository.selectOrderAmountByDateAndProductType("%Y-%m",time,pt.getId().intValue());
                }
                StatisticsModel statisticsModel=new StatisticsModel();
                statisticsModel.setSameMonthCount(sameMonthCount);
                statisticsModel.setTodayCount(todayCount);
                statisticsModel.setYesterdayCount(yesterdayCount);
                statisticsModel.setProductTypeName(pt.getProductTypeName());
                statisticsModel.setStockCount(stockCount);
                list.add(statisticsModel);
            }
        }
        return success(list,String.valueOf(monthCount));
    }

}


